package com.donggua.aichebao.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.third.login.ThirdLoginAuthListener;
import com.third.login.ThirdLoginBase;
import com.third.login.ThirdLoginData;
import com.third.login.ThirdLoginFactory;
import com.third.login.ThirdLoginPlatform;

/**
 * Created by fzh on 2017/8/4.
 */

 public class DGThirdLogin implements ThirdLoginAuthListener {
    protected Activity activity;
    protected ThirdLoginBase qqThirdLogin;
    protected ThirdLoginBase weiXinThirdLogin;
    protected ThirdLoginBase sinaThirdLogin;
    protected ThirdLoginPlatform thirdLoginPlatform;
    protected ThirdLoginBase currentLogin;
    public DGThirdLogin(Activity activity){
        this.activity = activity;
    }

    public void qqLogin(){
        thirdLoginPlatform = ThirdLoginPlatform.QQ;
        if(qqThirdLogin == null){
            qqThirdLogin = ThirdLoginFactory.getThridLogin("com.third.login.umeng.tencent.UQQThirdLogin",activity,"","");
         //   qqThirdLogin = new UQQThirdLogin(activity,"","");
            qqThirdLogin.setThirdLoginAuthListener(this);
        }
        if(qqThirdLogin != null){
            qqThirdLogin.thirdLogin();
        }
        Log.e("login"," qqThirdLogin is "+qqThirdLogin);
        currentLogin = qqThirdLogin;
    }

    public void weiXinLogin(){
        thirdLoginPlatform = ThirdLoginPlatform.WEIXIN;
        if(weiXinThirdLogin == null){
            weiXinThirdLogin = ThirdLoginFactory.getThridLogin("com.third.login.umeng.tencent.UWeiXinThirdLogin",activity,"","");
            weiXinThirdLogin.setThirdLoginAuthListener(this);
        }
        if(weiXinThirdLogin != null){
            weiXinThirdLogin.thirdLogin();
        }
        Log.e("login"," weiXinThirdLogin is "+weiXinThirdLogin);
        currentLogin = weiXinThirdLogin;
    }

    public void sinaLogin(){
        thirdLoginPlatform = ThirdLoginPlatform.SINA;
        if(sinaThirdLogin == null){
            sinaThirdLogin = ThirdLoginFactory.getThridLogin("com.third.login.umeng.sina.USinaThirdLogin",activity,"","");
            sinaThirdLogin.setThirdLoginAuthListener(this);
        }
        if(sinaThirdLogin != null){
            sinaThirdLogin.thirdLogin();
        }
        Log.e("login"," sinaLogin is "+sinaThirdLogin);
        currentLogin = sinaThirdLogin;
    }

    @Override
    public void onStart(ThirdLoginPlatform platform) {
    }

    @Override
    public void onComplete(ThirdLoginPlatform platform, int action, ThirdLoginData data) {
        Log.i("drj","platform" + platform);
        Log.i("drj","action" + action);
        Log.i("drj","data: " + data);
    }

    @Override
    public void onError(ThirdLoginPlatform platform, int action, String errorMsg, Throwable t) {

    }

    @Override
    public void onCancel(ThirdLoginPlatform platform, int action) {

    }

    public  void thirdLogin(){

    }
    public  void thirdLogout(){

    }
    public  void cancelLogin(){
        if(currentLogin != null){
            currentLogin.cancelLogin();
        }
    }
    public  void onActivityResult(int requestCode, int resultCode, Intent data){
        if(currentLogin != null){
            currentLogin.onActivityResult(requestCode,resultCode,data);
        }
    }
    public  void onDestroy(){
        if(qqThirdLogin != null){
            qqThirdLogin.onDestroy();
        }
        if(weiXinThirdLogin != null){
            weiXinThirdLogin.onDestroy();
        }
        if(sinaThirdLogin != null){
            sinaThirdLogin.onDestroy();
        }
    }
    public  void onSaveInstanceState(Bundle outState){
        if(currentLogin != null){
            currentLogin.onSaveInstanceState(outState);
        }
    }
}
